package {
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	
	import com.googlecode.milib.MILibMain;
	import com.googlecode.milib.animation.static_.MLEase;
	import com.googlecode.milib.data.value.MLNumberValue;
	import com.googlecode.milib.dbg.InstanceDebugServiceProxy;
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.service.singleton.KeyboardService;
	import com.googlecode.milib.util.MLMCUtil;
	import com.googlecode.milib.util.MLMathUtil;
	import com.googlecode.milib.vo.MLEvent;	

	/**
	 * @author Marek Brun 'minim'
	 */
	public class AnimatingExampleMain extends MovieClip {
		
		private var ks:KeyboardService;
		private var mc_simple : MovieClip;
		private var currentExampleNum:uint;		private var examplesSum:uint=2;
		private var currentExample:Object;
		private var currentExampleMC:MovieClip;
		private var dbg:InstanceDebugServiceProxy;
		
		public function AnimatingExampleMain() {
			
			//MILib must be initialized in most cases by passing root
			MILibMain.init(this);
			MILibMain.getInstance().setupStandardStage();
			
			//enabling debugger
			MLDBG.init();
			
			//prepare dynamically (in runtime) changed(able) eases and dynamic numbers
			setupEases();
			setupDynamicNumbers();
			
			//we not extending here MLBaseClass so we must create to handle this
			//instance debug tools
			dbg=InstanceDebugServiceProxy.forInstance(this);
			
			stage.frameRate=60;
			
			ks=KeyboardService.getInstance();
			ks.addEventListener(ks.event_Down, onKSDown);
			
			mc_simple=MovieClip(getChildByName('simple'));
			removeChild(mc_simple);
			
			setExample(0);
			
			dbg.log('Press key A or B to start animation');
		}
		
		protected function setExample(num:uint):void {
			if(currentExampleMC){ removeChild(currentExampleMC); }
			currentExample=null;
			switch(num){
				case 0:
					dbg.log('<b>static animation</b>');
					currentExampleMC=MovieClip(MLMCUtil.duplicateDisplayObject(mc_simple));
					addChild(currentExampleMC);
					currentExample=new AandBStatic(currentExampleMC);
				break;
				case 1:
					dbg.log('<b>dynamic animation</b>');
					currentExampleMC=MovieClip(MLMCUtil.duplicateDisplayObject(mc_simple));
					addChild(currentExampleMC);
					currentExample=new AandBDynamic(currentExampleMC);
				break;
			}
			currentExampleNum=num;
		}
		
		static public var ease_ToA : MLEase;
		static public var ease_ToB : MLEase;
		
		static public function setupEases():void {
			ease_ToA=MLEase.getInstanceForName('ToA').setupEaseElastic(8, 1.8, 0.22, 1);
			ease_ToB=MLEase.getInstanceForName('ToB').setupEaseBounce(5.42, 0.44, 0.63);
		}
		
		static public var dynamicNumber_ToA : MLNumberValue;
		static public var dynamicNumber_ToB : MLNumberValue;
		
		static public function setupDynamicNumbers():void {
			dynamicNumber_ToA=MLNumberValue.getInstanceForID("ToA");
			dynamicNumber_ToA.v=66;
			dynamicNumber_ToB=MLNumberValue.getInstanceForID("ToB");
			dynamicNumber_ToB.v=7778;
		}
		
//********************************************************************************************
//		events for AnimatingExampleMain
//********************************************************************************************
		public function onKSDown(event:MLEvent):void {
			switch(KeyboardEvent(event.info.data).keyCode) {
				case KeyboardService.KEY_S:
					setExample(MLMathUtil.getNextPrev(0, examplesSum-1, currentExampleNum, true));
				break;
			}
		}
		
	}
}
